/**
 * SeriesManagerController
 * @Chunning.Xu
 * 2011 Nov 23, 2011 5:14:28 PM
 **/
package com.lux.product.controllers
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Cubic;
	import com.lux.product.models.ProductModel;
	import com.lux.product.models.SeriesModel;
	import com.lux.product.swfaddress.SWFAddress;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	
	public class SeriesManagerController extends LuxViewController
	{
		//START.
		
		private var seriesList:Vector.<SeriesModel>;
		private var currentCtrl:SeriesController;
		private var desController:DesViewController;
		private var seqController:SequenceViewController;
		
		public var btnLeft:Sprite;
		public var btnRight:Sprite;
		public var seriesContainer:Sprite;
		public var seriesMask:DisplayObject;
		public var btnReturn:Sprite;
		public var btnPurchase:Sprite;
		public var desView:Sprite;
		public var knowMore:DisplayObject;
		public var sequenceView:Sprite;
		
		private const DES_SPACE:Number = 10;
		
		public function SeriesManagerController(view:Sprite,seriesList:Vector.<SeriesModel>)
		{
			this.seriesList = seriesList;
			super(view);
			
			initialize();
		}
		
		private function initialize():void
		{
			btnLeft.buttonMode = true;
			btnRight.buttonMode = true;
			btnReturn.buttonMode = true;
			btnPurchase.buttonMode = true;

			sequenceView.visible = false;
			knowMore.visible = false;
			btnLeft.visible = false;
			btnRight.visible = false;
			btnReturn.visible = false;
			
			desController = new DesViewController(desView);
			seqController = new SequenceViewController(sequenceView);
			
			btnLeft.addEventListener(MouseEvent.CLICK,btnLeftClickHandler);
			btnRight.addEventListener(MouseEvent.CLICK,btnRightClickHandler);
			btnReturn.addEventListener(MouseEvent.CLICK,btnReturnClickHandler);
			btnPurchase.addEventListener(MouseEvent.CLICK,btnPurchaseClickHandler);
		}
		
		private function btnPurchaseClickHandler(evt:Event):void
		{
			if(currentCtrl == null)
				return;
			
			if(currentCtrl.index < 0 && currentCtrl.series.purchase != null)
			{
				navigateToURL(new URLRequest(currentCtrl.series.purchase),"_blank");
				return;
			}
			
			if(currentCtrl.index >= 0 && currentCtrl.series.products[currentCtrl.index].purchase != null)
				navigateToURL(new URLRequest(currentCtrl.series.products[currentCtrl.index].purchase),"_blank");
		}
		
		private function btnLeftClickHandler(evt:Event):void
		{
		 	if(currentCtrl == null)
				return;
			var index:int = seriesList.indexOf(currentCtrl.series);
			if(index <= 0)
				return;
			
			SWFAddress.setValue(seriesList[index - 1].label);
		}
		
		private function btnRightClickHandler(evt:Event):void
		{
			if(currentCtrl == null)
				return;
			var index:int = seriesList.indexOf(currentCtrl.series);
			if(index >= seriesList.length - 1)
				return;
			
			SWFAddress.setValue(seriesList[index + 1].label);
		}
		
		private function btnReturnClickHandler(evt:Event):void
		{
			if(currentCtrl == null)
				return;
			if(currentCtrl.index < 0)
				return;
			
			SWFAddress.setValue(currentCtrl.series.label);
		}
		
		private function easeComplete(view:DisplayObject):void
		{
			if(seriesContainer.contains(view))
				seriesContainer.removeChild(view);
		}
		
		public function setSModelAndPModel(sModel:SeriesModel,pModel:ProductModel = null):void
		{
			var url:String = ExternalInterface.call("window.location.href.toString");
			seqController.sequence = sModel.squence;
			if(pModel != null)
			{
				btnLeft.visible = false;
				btnRight.visible = false;
				btnReturn.visible = true;
				knowMore.visible = false;
				sequenceView.visible = false;
				
				desController.changeCopy(pModel.title,pModel.des);
				ExternalInterface.call("showTip",pModel.tip,pModel.share,url);
				if(pModel.tracking.pageView != null)
					ExternalInterface.call("_gaq.push",["_trackPageview",pModel.tracking.pageView]);
			}else{
				btnLeft.visible = seriesList.indexOf(sModel) > 0;
				btnRight.visible = seriesList.indexOf(sModel) < seriesList.length - 1;
				btnReturn.visible = false;
				knowMore.visible = true;
				sequenceView.visible = true;
				
				if(btnLeft.visible)
				{
					var txtLeft:TextField = btnLeft.getChildByName("txt") as TextField;
					txtLeft.text =  seriesList[seriesList.indexOf(sModel) - 1].title;
				}
				
				if(btnRight.visible)
				{
					var txtRight:TextField = btnRight.getChildByName("txt") as TextField;
					txtRight.text = seriesList[seriesList.indexOf(sModel) + 1].title;
				}
				
				desController.changeCopy(sModel.title,sModel.des);
				ExternalInterface.call("showTip",sModel.tip,sModel.share,url);
				if(sModel.tracking.pageView != null)
					ExternalInterface.call("_gaq.push",["_trackPageview",sModel.tracking.pageView]);
			}

			TweenLite.to(btnPurchase,0.5,{ease:Cubic.easeInOut,y:desController.view.y + desController.contentHeight + DES_SPACE});
			TweenLite.to(seqController.view,0.5,{ease:Cubic.easeInOut,y:desController.view.y + desController.contentHeight + DES_SPACE + btnPurchase.height / 2});
			
			if(currentCtrl != null)
			{
				if(currentCtrl.series == sModel)
				{
					if(pModel != null)
						currentCtrl.setIndex(sModel.products.indexOf(pModel),true);
					else if(currentCtrl.index > -1)
						currentCtrl.setIndex(-1,true);
				}else{
					var newCtrl:SeriesController = new SeriesController(sModel);
					if(pModel != null)
						newCtrl.setIndex(sModel.products.indexOf(pModel));
					
					var newX:Number;
					var oldX:Number;
					if(seriesList.indexOf(sModel) > seriesList.indexOf(currentCtrl.series))
					{
						newX = seriesMask.getRect(seriesContainer).right - newCtrl.view.getRect(newCtrl.view).left;
						oldX = seriesMask.getRect(seriesContainer).left  - currentCtrl.view.getRect(currentCtrl.view).right;
						
					}else{
						newX = seriesMask.getRect(seriesContainer).left - newCtrl.view.getRect(newCtrl.view).right;
						oldX = seriesMask.getRect(seriesContainer).right  - currentCtrl.view.getRect(currentCtrl.view).left;
					}
					
					newCtrl.view.x = newX;
					seriesContainer.addChild(newCtrl.view);
					
					TweenLite.to(newCtrl.view,1.0,{x:0,ease:Cubic.easeInOut});
					TweenLite.to(currentCtrl.view,1.0,{x:oldX,ease:Cubic.easeInOut,onComplete:easeComplete,onCompleteParams:[currentCtrl.view]});	
					
					currentCtrl = newCtrl;
				}
				
			}else{
				currentCtrl = new SeriesController(sModel);
				if(pModel != null)
					currentCtrl.setIndex(sModel.products.indexOf(pModel));					
				seriesContainer.addChild(currentCtrl.view);
			}
		}
		
		//END
	}
}